package spe.ui.properties;

import java.beans.PropertyChangeListener;

import org.eclipse.gef.commands.CommandStack;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.IPage;
import org.eclipse.ui.views.contentoutline.ContentOutline;
import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection;

import spe.model.spem.SPEMElement;
import spe.ui.editor.SPEEditor;
import spe.ui.editor.SPEOutlineViewer;
import spe.ui.editor.part.NodeElementEditPart;

public abstract class PropertySection extends AbstractPropertySection  implements PropertyChangeListener
{
	protected SPEEditor editor;
	protected SPEMElement element;
	
	@Override
	public void setInput(IWorkbenchPart part, ISelection selection)
	{
		super.setInput(part, selection);
		if(part instanceof SPEEditor)
		{
			editor = (SPEEditor) part;
			Object input;
			if(selection instanceof IStructuredSelection)
			{
				input = ((IStructuredSelection)selection).getFirstElement();
				if(input instanceof NodeElementEditPart)
				{
					element = (SPEMElement) ((NodeElementEditPart)input).getModel();
					element.addPropertyChangeListener(this);
					//System.out.println(String.format("[PropertySection] Part: %s; element: %s", part, element));
				}
				
			}
		}
		
		if(part instanceof ContentOutline)
		{
			IPage page = ((ContentOutline)part).getCurrentPage();
			if(page instanceof SPEOutlineViewer)
				editor = ((SPEOutlineViewer)page).getEditor();
		}
		
		initControlsValues();
	}
	
	public CommandStack getCommandStack()
	{
		return editor==null? null : editor.getCommandStack();
	}
	
	protected abstract void initControlsValues();
}
